home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / v cisle / htttrack / httrack-3.41-3.exe / {app} / src / htsstrings.h < prev    next >
C/C++ Source or Header  |  2006-08-15  |  8KB  |  283 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: Strings                                                */
  34. /* Author: Xavier Roche                                         */
  35. /* ------------------------------------------------------------ */
  36.  
  37. /* Safer Strings ; standalone .h library */
  38.  
  39. #ifndef HTS_STRINGS_DEFSTATIC
  40. #define HTS_STRINGS_DEFSTATIC 
  41.  
  42. /* System definitions. */
  43. #include <string.h>
  44.  
  45. /* GCC extension */
  46. #ifndef HTS_UNUSED
  47. #ifdef __GNUC__
  48. #define HTS_UNUSED __attribute__ ((unused))
  49. #define HTS_STATIC static __attribute__ ((unused))
  50. #else
  51. #define HTS_UNUSED
  52. #define HTS_STATIC static
  53. #endif
  54. #endif
  55.  
  56. /** Forward definitions **/
  57. #ifndef HTS_DEF_FWSTRUCT_String
  58. #define HTS_DEF_FWSTRUCT_String
  59. typedef struct String String;
  60. #endif
  61. #ifndef HTS_DEF_STRUCT_String
  62. #define HTS_DEF_STRUCT_String
  63. struct String {
  64.   char* buffer_;
  65.   size_t length_;
  66.   size_t capacity_;
  67. };
  68. #endif
  69.  
  70. /** Allocator **/
  71. #ifndef STRING_REALLOC
  72. #define STRING_REALLOC(BUFF, SIZE) ( (char*) realloc(BUFF, SIZE) )
  73. #define STRING_FREE(BUFF) free(BUFF)
  74. #endif
  75. #ifndef STRING_ASSERT
  76. #include <assert.h>
  77. #define STRING_ASSERT(EXP) assert(EXP)
  78. #endif
  79.  
  80. /** An empty string **/
  81. #define STRING_EMPTY { (char*) NULL, 0, 0 }
  82.  
  83. /** String buffer **/
  84. #define StringBuff(BLK) ( (const char*) ((BLK).buffer_) )
  85.  
  86. /** String buffer (read/write) **/
  87. #define StringBuffRW(BLK) ((BLK).buffer_)
  88.  
  89. /** String length **/
  90. #define StringLength(BLK) ((BLK).length_)
  91.  
  92. /** String not empty ? **/
  93. #define StringNotEmpty(BLK) ( StringLength(BLK) > 0 )
  94.  
  95. /** String capacity **/
  96. #define StringCapacity(BLK) ((BLK).capacity_)
  97.  
  98. /** Subcharacter **/
  99. #define StringSub(BLK, POS) ( StringBuff(BLK)[POS] )
  100.  
  101. /** Subcharacter (read/write) **/
  102. #define StringSubRW(BLK, POS) ( StringBuffRW(BLK)[POS] )
  103.  
  104. /** Subcharacter (read/write) **/
  105. #define StringSubRW(BLK, POS) ( StringBuffRW(BLK)[POS] )
  106.  
  107. /** Right subcharacter **/
  108. #define StringRight(BLK, POS) ( StringBuff(BLK)[StringLength(BLK) - POS] )
  109.  
  110. /** Right subcharacter (read/write) **/
  111. #define StringRightRW(BLK, POS) ( StringBuffRW(BLK)[StringLength(BLK) - POS] )
  112.  
  113. /** Remove the utter right character from the string. **/
  114. #define StringPopRight(BLK) do { \
  115.   StringBuffRW(BLK)[--StringLength(BLK)] = '\0'; \
  116. } while(0)
  117.  
  118. /** Ensure the string is large enough **/
  119. #define StringRoom(BLK, SIZE) do { \
  120.   if ((BLK).length_ + (int)(SIZE) + 1 > (BLK).capacity_) { \
  121.     (BLK).capacity_ = ((BLK).length_ + (SIZE) + 1) * 2; \
  122.     (BLK).buffer_ = (char*) STRING_REALLOC((BLK).buffer_, (BLK).capacity_); \
  123.     STRING_ASSERT((BLK).buffer_ != NULL); \
  124.   } \
  125. } while(0)
  126. #define StringBuffN(BLK, SIZE) StringBuffN_(&(BLK), SIZE) 
  127. HTS_STATIC char* StringBuffN_(String* blk, int size) {
  128.   StringRoom(*blk, StringLength(*blk) + size);
  129.   return StringBuffRW(*blk);
  130. }
  131.  
  132. /** Initialize a string. **/
  133. #define StringInit(BLK) do { \
  134.   (BLK).buffer_ = NULL; \
  135.   (BLK).capacity_ = 0; \
  136.   (BLK).length_ = 0; \
  137. } while(0)
  138.  
  139. /** Clear a string (set its length to 0) **/
  140. #define StringClear(BLK) do { \
  141.   StringRoom(BLK, 0); \
  142.   (BLK).buffer_[0] = '\0'; \
  143.   (BLK).length_ = 0; \
  144. } while(0)
  145.  
  146. /** Set the length of a string to 'SIZE'. If SIZE is negative, check the size using strlen(). **/
  147. #define StringSetLength(BLK, SIZE) do { \
  148.   if (SIZE >= 0) { \
  149.     (BLK).length_ = SIZE; \
  150.   } else { \
  151.     (BLK).length_ = strlen((BLK).buffer_); \
  152.   } \
  153. } while(0)
  154.  
  155. /** Free a string (release memory) **/
  156. #define StringFree(BLK) do { \
  157.   if ((BLK).buffer_ != NULL) { \
  158.     STRING_FREE((BLK).buffer_); \
  159.     (BLK).buffer_ = NULL; \
  160.   } \
  161.   (BLK).capacity_ = 0; \
  162.   (BLK).length_ = 0; \
  163. } while(0)
  164.  
  165. /** Assign an allocated pointer to a a string.
  166. The pointer _MUST_ be compatible with STRING_REALLOC() and STRING_FREE() **/
  167. #define StringSetBuffer(BLK, STR) do { \
  168.   size_t len__ = strlen( STR ); \
  169.   StringFree(BLK); \
  170.   (BLK).buffer_ = ( STR ); \
  171.   (BLK).capacity_ = len__; \
  172.   (BLK).length_ = len__; \
  173. } while(0)
  174.  
  175. /** Append a memory block to a string **/
  176. #define StringMemcat(BLK, STR, SIZE) do { \
  177.   StringRoom(BLK, SIZE); \
  178.   if ((int)(SIZE) > 0) { \
  179.     memcpy((BLK).buffer_ + (BLK).length_, (STR), (SIZE)); \
  180.     (BLK).length_ += (int)(SIZE); \
  181.   } \
  182.   *((BLK).buffer_ + (BLK).length_) = '\0'; \
  183. } while(0)
  184.  
  185. /** Copy a memory block to a string **/
  186. #define StringMemcpy(BLK, STR, SIZE) do { \
  187.   (BLK).length_ = 0; \
  188.   StringMemcat(BLK, STR, SIZE); \
  189. } while(0)
  190.  
  191. /** Add a character **/
  192. #define StringAddchar(BLK, c) do { \
  193.   String * const s__ = &(BLK); \
  194.   char c__ = (c); \
  195.   StringRoom(*s__, 1); \
  196.   StringBuffRW(*s__)[StringLength(*s__)++] = c__; \
  197.   StringBuffRW(*s__)[StringLength(*s__)  ] = 0; \
  198. } while(0)
  199.  
  200. /** Acquire a string ; it's the client's responsability to free() it **/
  201. HTS_STATIC char* StringAcquire(String* blk) {
  202.   char* buff = StringBuffRW(*blk);
  203.   StringBuffRW(*blk) = NULL;
  204.   StringCapacity(*blk) = 0;
  205.   StringLength(*blk) = 0;
  206.   return buff;
  207. }
  208.  
  209. /** Clone a string. **/
  210. HTS_STATIC String StringDup(const String* src) {
  211.   String s = STRING_EMPTY;
  212.   StringMemcat(s, StringBuff(*src), StringLength(*src));
  213.   return s;
  214. }
  215.  
  216. /** Attach a string using a pointer. **/
  217. HTS_STATIC void StringAttach(String* blk, char** str) {
  218.   StringFree(*blk);
  219.   if (str != NULL && *str != NULL) {
  220.     StringBuffRW(*blk) = *str;
  221.     StringCapacity(*blk) = StringLength(*blk) = strlen(StringBuff(*blk));
  222.     *str = NULL;
  223.   }
  224. }
  225.  
  226. /** Append a string to another one. **/
  227. #define StringCat(BLK, STR) do { \
  228.   const char *str__ = ( STR ); \
  229.   if (str__ != NULL) { \
  230.     size_t size__ = strlen(str__); \
  231.     StringMemcat(BLK, str__, size__); \
  232.   } \
  233. } while(0)
  234.  
  235. #define StringCatN(BLK, STR, SIZE) do { \
  236.   const char *str__ = ( STR ); \
  237.   if (str__ != NULL) { \
  238.     size_t size__ = strlen(str__); \
  239.     if (size__ > (SIZE)) { \
  240.       size__ = (SIZE); \
  241.     } \
  242.     StringMemcat(BLK, str__, size__); \
  243.   } \
  244. } while(0)
  245.  
  246. #define StringCopyN(BLK, STR, SIZE) do { \
  247.   const char *str__ = ( STR ); \
  248.   const size_t usize__ = (SIZE); \
  249.   (BLK).length_ = 0; \
  250.   if (str__ != NULL) { \
  251.     size_t size__ = strlen(str__); \
  252.     if (size__ > usize__ ) { \
  253.       size__ = usize__; \
  254.     } \
  255.     StringMemcat(BLK, str__, size__); \
  256.   } else { \
  257.     StringClear(BLK); \
  258.   } \
  259. } while(0)
  260.  
  261. #define StringCopyS(blk, blk2) StringCopyN(blk, (blk2).buffer_, (blk2).length_)
  262.  
  263. /** Copy a string to another one. **/
  264. #define StringCopy(BLK, STR) do { \
  265.   const char *str__ = ( STR ); \
  266.   if (str__ != NULL) { \
  267.     size_t size__ = strlen(str__); \
  268.     StringMemcpy(BLK, str__, size__); \
  269.   } else { \
  270.     StringClear(BLK); \
  271.   } \
  272. } while(0)
  273.  
  274. /** Copy a (potentially overlapping) string to another one. **/
  275. #define StringCopyOverlapped(BLK, STR) do { \
  276.   String s__ = STRING_EMPTY; \
  277.   StringCopy(s__, STR); \
  278.   StringCopyS(BLK, s__); \
  279.   StringFree(s__); \
  280. } while(0)
  281.  
  282. #endif
  283.